import { utilDeepClone } from "@/assets/utils/deepClone";
import useClipboard from "vue-clipboard3";
export function utilGetCookie(name: string) {
  let arr;
  const reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
  if ((arr = document.cookie.match(reg))) return decodeURI(arr[2]);
  else return null;
}
export function utilSetCookie(name: string, value: any) {
  const Days = 30;
  const exp = new Date();
  exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);
  document.cookie = name + "=" + value + ";expires=" + exp["toUTCString"]() + ";path=/";
}
export function utilDelCookie(name: string) {
  const exp = new Date();
  exp.setTime(exp.getTime() - 1);
  const cval = utilGetCookie(name);
  if (cval != null) document.cookie = name + "=" + cval + ";expires=" + exp["toUTCString"]() + ";path=/";
}
export function utilParseTime(time: string | number, cFormat?: string) {
  if (arguments.length === 0) {
    return null;
  }
  if ((time + "").length === 10) {
    time = +time * 1000;
  }
  const format = cFormat || "{y}-{m}-{d}";
  let date;
  if (typeof time === "object") {
    date = time;
  } else {
    date = new Date(parseInt(time + ""));
  }
  const formatObj = {
    y: date.getFullYear(),
    m: date.getMonth() + 1,
    d: date.getDate(),
    h: date.getHours(),
    i: date.getMinutes(),
    s: date.getSeconds(),
    a: date.getDay()
  };
  const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result: string, key: keyof typeof formatObj) => {
    let value: string | number;
    value = formatObj[key];
    if (key === "a") return ["一", "二", "三", "四", "五", "六", "日"][value - 1];
    if (value < 10) value = "0" + value;
    return value + "" || "0";
  });
  return time_str;
}
export async function utilCopyLink(Msg: string) {
  const { toClipboard } = useClipboard();
  try {
    //复制
    await toClipboard(Msg);
    ElMessage.success("复制成功");
  } catch (e) {
    //复制失败
    ElMessage.error("无法复制");
  }
}
export function utilIsMobile() {
  return /(iPhone|iPad|iPod|iOS|Android|Linux armv8l|Linux armv7l|Linux aarch64)/i.test(navigator.userAgent);
}
// 去除value为null false '' undefind的值
export function utilClearEmptyKey(obj: any): any {
  let objHandle: any;
  for (let i in obj) {
    if (!objHandle) {
      if (Object.prototype.toString.call(obj) === "[object Object]") objHandle = {};
      if (Object.prototype.toString.call(obj) === "[object Array]") objHandle = [];
    }
    if (Object.prototype.toString.call(obj[i]) === "[object Object]") {
      objHandle[i] = utilClearEmptyKey(obj[i]);
    } else if (Object.prototype.toString.call(obj[i]) === "[object Array]") {
      if (!objHandle[i]) objHandle[i] = [];
      obj[i].map((item: any, index: number) => {
        objHandle[i][index] = utilClearEmptyKey(item);
      });
    } else {
      if (obj[i]) objHandle[i] = obj[i];
    }
  }
  return objHandle;
}
export function utilCombineObj(obj1: any, obj2: any, isOrign = false): any {
  if (obj1 === obj2) {
    console.log("utilCombineObj传入的对象相同");
    return obj1;
  }
  let handleObj = obj1 ? obj1 : obj2;
  let obj = isOrign ? handleObj : utilDeepClone(handleObj);
  for (let item in obj2) {
    if (Object.prototype.toString.call(obj[item]) === "[object Object]") {
      obj[item] = utilCombineObj(obj[item], obj2[item], isOrign);
      return obj;
    } else if (Object.prototype.toString.call(obj[item]) === "[object Array]") {
      obj[item].map((ite: any) => {
        return utilCombineObj(obj[item][ite], obj2[item][ite], isOrign);
      });
      return obj;
    }
    obj[item] = obj2[item] !== undefined ? obj2[item] : obj[item];
  }
  return obj;
}
export function utilisObject(value: any) {
  const valueType = typeof value;
  return value !== null && (valueType === "object" || valueType === "function");
}
// 可缓存的方法
export function utilComputed(fn:(...key:any)=>void) {
  const cache = new Map();
  function compare(arg1: any, arg2: any) {
    return arg1.length === arg2.length && arg1.every((item: any, index: number) => Object.is(item, arg2[index]));
  }
  function getCashe(args: any) {
    const keys = [...cache.keys()];
    const value= keys.find((item) => compare(item, args));
    if(value){
      return cache.get(value);
    }
  }
  return function (...args:any) {
    const cachedResult = getCashe(args);
    if (cachedResult) {
    return cachedResult.value;
    }
    const result = computed(() => fn(...args));
    cache.set(args, result);
    return result.value;
  };
}
